package com.woniuxy.util;


import java.util.ArrayList;
import java.util.List;

/**
 * 该方法用于分页
 */
public class CutPagesUtil {
    private static int currentPage = 1;
    private static int pageSize = 20;
    private static int totalPage = -1;
    private CutPagesUtil(){}

    /**
     * 该方法用于设置当前页码和每页条数
     * @param currentPage   当前页码
     * @param pageSize      每页显示条数
     */
    public static void setPageInfo(int currentPage,int pageSize){
        CutPagesUtil.currentPage = currentPage;
        CutPagesUtil.pageSize = pageSize;
    }

    /**
     * 获取分页信息
     * @param totalPages    你要传入的list
     * @param <T>
     * @return              当前页面的list
     */
    public static <T> List<T> getPages(List<T> totalPages){

        int totalPagesNum = totalPages.size();

        if ((totalPagesNum%pageSize)>0){
            totalPage = totalPagesNum/pageSize + 1;
        }else {
            totalPage = totalPagesNum/pageSize;
        }
        List<T> pages = new ArrayList<>();
        int num;
        if (currentPage*pageSize>totalPagesNum){
            num=totalPagesNum;
        }else {
            num=currentPage*pageSize;
        }
        for (int i = (currentPage-1)*pageSize;i<num;i++){
            pages.add(totalPages.get(i));
        }
        return pages;
    }

    /**
     * 获取总页数
     * @return  返回总页数
     */
    public static int getTotalPage(){
        return CutPagesUtil.totalPage;
    }

    /**
     * 清除之前设置
     */
    public static void clear(){
        CutPagesUtil.currentPage = 1;
        CutPagesUtil.pageSize = 20;
        CutPagesUtil.totalPage = -1;
    }
}
